package com.xxl.job.admin.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * Quartz配置类
 *
 * @author: 
 * @since: 2016-6-2 20:09
 */
@Configuration
public class QuartzConfig {
    private static final Logger log = LoggerFactory.getLogger(QuartzConfig.class);

    @Autowired
    private DataSource dataSource;


    @PostConstruct
    public void initDone() {
        log.info("Quartz init done...");
    }

    @Bean(name="quartzScheduler")
    public SchedulerFactoryBean init(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setAutoStartup(true);   //<!--自动启动 -->
        schedulerFactoryBean.setStartupDelay(20);     //<!--延时启动，应用启动成功后在启动 -->
        schedulerFactoryBean.setOverwriteExistingJobs(true);   //<!--覆盖DB中JOB：true、以数据库中已经存在的为准：false -->
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
        return schedulerFactoryBean;
    }

}
